using UnityEngine;
using System.Collections;

public class MainRoutine : MonoBehaviour
{
	private SphereScript sphere;
	private Destroyer destroyer;
	private Builder builder;
	const int STATE_TITLE = 1;
	const int STATE_MAINMENU = 2;
	const int STATE_PLAYING = 3;
	const int STATE_WIN = 4;
	private int state = STATE_TITLE;
	
	public Texture manual;
	public Texture title;
	
	public Texture icon_none;
	public Texture icon_slowDown;
	public Texture icon_speedUp;
	public Texture icon_multify;
	
	public Texture win_destroyer;
	public Texture win_builder;
	
	private Player lastWinner;
	
	// Use this for initialization
	void Start ()
	{
		Screen.showCursor=false;
		sphere = GameObject.Find("Sphere").GetComponent<SphereScript>();
		if(sphere == null)
			throw new MissingComponentException("SphereScript not found");
		destroyer = GameObject.Find("Destroyer").GetComponent<Destroyer>();
		builder = GameObject.Find("Builder").GetComponent<Builder>();
		
		
		//Init CommandSources
		destroyer.setCommandSource(new CommandSource());
		
		builder.setCommandSource(new CommandSource());
		Debug.Log("started");
		if(destroyer.getCommandSource() == null)
			throw new UnityException("Attachment of CommandSource didn't work");
	}
	public void OnGUI()
	{
		if (state == STATE_TITLE)
			GUI.DrawTexture(new Rect(Screen.width/2-512,Screen.height/2-768/2,1024,768), title);
		
		else if (state == STATE_MAINMENU)
			GUI.DrawTexture(new Rect(Screen.width/2-512,Screen.height/2-768/2,1024,768), manual);
		
		else if(state == STATE_PLAYING)
		{
			GUI.TextArea(new Rect(Screen.width/2 - 100f, 0, 200f, 50f), "Eggs placed: " + builder.getEggs().Count + "/" + Balance.WINCOND_BLOCKS_TARGET + "\nEggs left: " + builder.blocksLeft);
			GUI.DrawTexture(new Rect(5, 5, 128, 128), getItemTexture(builder.getItem()));
			GUI.DrawTexture(new Rect(Screen.width - 5 - 128, 5, 128, 128), getItemTexture(destroyer.getItem()));
		}
		else if (state == STATE_WIN)
			if(lastWinner is Destroyer)
				GUI.DrawTexture(new Rect(Screen.width/2-512,Screen.height/2-768/2,1024,768), win_destroyer);
			else
				GUI.DrawTexture(new Rect(Screen.width/2-512,Screen.height/2-768/2,1024,768), win_builder);
	}
	private Texture getItemTexture(Item item)
	{
		if(item == Item.None)
			return icon_none;
		
		if(item == Item.SlowDown)
			return icon_slowDown;
		
		if(item == Item.SpeedUp)
			return icon_speedUp;
		
		if(item == Item.Multify)
			return icon_multify;
		
		return null;
	}
	
	/**
	  * Update is called once per frame
	  * @todo key strokes by floats instead of booleans
	  **/
	public void Update ()
	{
		if(state == STATE_TITLE)
		{
			if(Input.GetKeyDown(UnityEngine.KeyCode.Return))
			   state = STATE_MAINMENU;
		}
		else if(state == STATE_MAINMENU)
		{
			if(Input.GetKeyDown(UnityEngine.KeyCode.Return))
			   startGame();
		}
		else if(state == STATE_WIN)
		{
			if(Input.GetKey(UnityEngine.KeyCode.Return))
			   startGame();
			else if(Input.GetKey(UnityEngine.KeyCode.Escape))
			   exitGame();
		}
	    else if(state == STATE_PLAYING)
		{
			
			if(Input.GetKey("q"))
				builder.getCommandSource().primarySkill = true;
			else
			{
				if(Input.GetKey("w"))
					builder.getCommandSource().moveForward = true;
				if(Input.GetKey("a"))
					builder.getCommandSource().rotateLeft = true;
				if(Input.GetKey("s"))
					builder.getCommandSource().moveBackward = true;
				if(Input.GetKey("d"))
					builder.getCommandSource().rotateRight = true;
				if(Input.GetKey("q"))
					builder.getCommandSource().primarySkill = true;
				if(Input.GetKey("e"))
					builder.getCommandSource().secondarySkill = true;
			}
			if(Input.GetKey("i"))
				destroyer.getCommandSource().moveForward = true;
			if(Input.GetKey("j"))
				destroyer.getCommandSource().rotateRight = true;
			if(Input.GetKey("k"))
				destroyer.getCommandSource().moveBackward = true;
			if(Input.GetKey("l"))
				destroyer.getCommandSource().rotateLeft = true;
			if(Input.GetKey("u"))
				destroyer.getCommandSource().primarySkill = true;
			if(Input.GetKey("o"))
				destroyer.getCommandSource().secondarySkill = true;
		
			
			if(Input.GetKey(UnityEngine.KeyCode.Escape))
			   exitGame();
			
			checkForVictoryConditions();
		}
		
	}
	private void checkForVictoryConditions()
	{
		if(builder.isEaten())
			showScoreScreen(destroyer);
		
		if(builder.getEggs().Count >= Balance.WINCOND_BLOCKS_TARGET)
			showScoreScreen(builder);
	}
	private void startGame()
	{
		float r = Balance.SPHERE_RADIUS;
		sphere.transform.localScale = new Vector3(2 * r, 2 * r, 2 * r);
		
		destroyer.transform.position = new Vector3(0, - r - Balance.DESTROYER_Y_OFFSET, 0);
		destroyer.transform.rotation = Quaternion.Euler(Vector3.zero);
		destroyer.reset();
		
		builder.transform.position = new Vector3(0, r + Balance.BUILDER_Y_OFFSET, 0);
		builder.transform.rotation = Quaternion.Euler(Vector3.zero);
		builder.reset();
		
		Debug.Log("start");
		state = STATE_PLAYING;
	}
	private void exitGame()
	{
		Debug.Log("Exit");
		Application.Quit();
	}
	
	private void showScoreScreen(Player winner)
	{
		lastWinner = winner;
		state = STATE_WIN;
	}
	
}
